import 'package:flutter/material.dart';
import 'package:flutter_stort/html/collect_dao.dart';
import 'package:flutter_stort/model/collect_entity.dart';
import 'package:flutter_stort/widget/load_image.dart';
import 'package:flutter_stort/widget/load_state_view.dart';

///我的收藏
class CollectPage extends StatefulWidget {
  @override
  _CollectPageState createState() => _CollectPageState();
}

class _CollectPageState extends State<CollectPage> {
  int page = 1;
  LoadState _loadState = LoadState.LOADING;
  List<CollectEntity> _listCollect = [];
  ScrollController _listController = ScrollController();

  @override
  void initState() {
    _getData();
    _listController.addListener(() {
      //判断是否滑动到了页面的最底部
      if (_listController.position.pixels ==
          _listController.position.maxScrollExtent) {
        page++;
        Future.delayed(Duration(seconds: 3)).then((_) {
          _getData();
        });
      }
    });

    super.initState();
  }

  @override
  void dispose() {
    _listController.dispose();
    super.dispose();
  }

  void _getData() {
    ///获取收藏数据
    CollectDao.getCollect().then((value) {
      setState(() {
        _listCollect.addAll(value);
        _loadState = LoadState.SUCCESS;
      });
    });
  }

  ///刷新数据
  Future<void> _onRefresh() async {
    await Future.delayed(Duration(seconds: 3)).then((_) {
      page = 1;
      _listCollect.clear();
      _getData();
    });
  }

  ///收藏的item
  Widget _collectItemView(BuildContext context, int index) {
    if (index == _listCollect.length) {
      return Container(
        padding: EdgeInsets.all(16.0),
        alignment: Alignment.center,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(
              width: 24.0,
              height: 24.0,
              child: CircularProgressIndicator(
                strokeWidth: 2.0,
              ),
            ),
            SizedBox(width: 10),
            Text(
              "加载中......",
              style: TextStyle(
                color: Colors.blue,
                fontSize: 20,
              ),
            ),
          ],
        ),
      );
    } else {
      return Container(
        padding: EdgeInsets.all(10),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            LoadImageView(
              imageUrl: _listCollect[index].imgUrl,
              width: 90,
              height: 90,
              fit: BoxFit.cover,
            ),
            SizedBox(width: 10),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "${_listCollect[index].title}",
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 4),
                  Text(
                    "收藏 ${_listCollect[index].collectionNum}次",
                    style: TextStyle(
                      color: Colors.grey,
                      fontSize: 12,
                    ),
                  ),
                  SizedBox(height: 4),
                  Text(
                    "￥ ${_listCollect[index].price}",
                    style: TextStyle(
                      color: Colors.red,
                      fontSize: 14,
                    ),
                  ),
                  Row(
                    children: [],
                  ),
                ],
              ),
            ),
          ],
        ),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Colors.grey,
              width: 0.5,
              style: BorderStyle.solid,
            ),
          ),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "我的收藏",
          style: TextStyle(
            fontSize: 20,
            color: Colors.white,
          ),
        ),
      ),
      body: LoadStateView(
        loadState: _loadState,
        erroRetry: () {},
        successWidget: RefreshIndicator(
          onRefresh: _onRefresh,
          child: ListView.builder(
            controller: _listController,
            itemBuilder: _collectItemView,
            itemCount: _listCollect.length + 1,
          ),
        ),
      ),
    );
  }
}
